<?php

function cnpscy_config(string $config_name = '', string $default = '')
{
    return \App\Library\Config::get($config_name, $default);
}

function view(string $template, array $vars = [])
{

}

/**
 *
 *
 * Laravel内部方法
 *
 */
function url($string, $params = [])
{
    return $string . '?' . http_build_query($params);
}

function asset($string)
{
    return $string;
}

function trans($string)
{
    return [];
}

/**
 * 获取模块名称
 * @return string
 */
function get_module_name():string
{
    return '';
    return strtolower(str_replace(
            'App\\Modules\\',
            '',
            trim(
                implode('\\', array_slice(explode('\\', request()->route()->getAction('namespace')), 0, -2)),
                '\\'
            )
        ) ?? '');
}

function get_web_admin_url(string $method, $params = [], string $controller = '', int $is_compel = 1): string
{
    $prefix = \App\Library\Config::get('_server.request_uri');

    //var_dump(last(explode('/', $prefix)));

    if (empty($controller) && $is_compel == 0) {
        $url = $prefix . '/' . ltrim($method, '/');
    } else {
        $url = str_replace(last(explode('/', $prefix)), $controller, $prefix) . (empty($controller) ? '' : '/') . ltrim($method, '/');
    }
    return url($url, $params);
}

function get_api_admin_url(string $method, string $controller = '', int $is_compel = 1)
{
    $prefix = \App\Library\Config::get('_server.request_uri');

    if ($is_compel == 1) {
        $url = empty($controller)
            ? 'api/' . $prefix . '/' . ltrim($method, '/')
            : 'api/' . $prefix . (empty($controller) ? '' : '/' . $controller . '/') . ltrim($method, '/');
    } else {
        $prefix = ltrim($request_uri, '/');
        $prefix = str_replace(head(explode('/', $prefix)), cnpscy_config('api_admin_prefix'), $prefix);
        $url    = 'api/' . (empty($controller) ? $prefix : str_replace(last(explode('/', $prefix)), $controller, $prefix)) . '/' . ltrim($method, '/');
    }
    return url($url);
}

function get_model_url(string $method, string $controller = '', int $is_compel = 0)
{
    return '';
    if (empty($method)) return url('/');
    $prefix = ltrim($request_uri, '/');
    if (empty($controller) && $is_compel == 0) {
        $url = $prefix . '/' . ltrim($method, '/');
    } else {
        $url = str_replace(last(explode('/', $prefix)), $controller, $prefix) . (empty($controller) ? '' : '/') . $method;
    }
    return url($url);
}